package com.ys.otms.services.impl;

import com.ys.otms.dao.IConfigDao;
import com.ys.otms.dto.ConfigDto;
import com.ys.otms.dto.ParamDto;
import com.ys.otms.services.IConfigServices;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.ListUtil;
import com.ys.otms.utils.PageUtil;
import com.ys.otms.utils.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ConfigServiceImpl implements IConfigServices {

    private static Logger logger = LoggerFactory.getLogger(ConfigServiceImpl.class);

    @Autowired
    private IConfigDao iConfigDao;

    @Override
    public JSONObject addGoodsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl addGoodsConfig start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        ConfigDto configDto = new ConfigDto();
        String goodsName = JsonUtil.getJSONObjectString2(obj, "goodsName");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(goodsName)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "参数为空");
            return retObj;
        }
        configDto.setGoodsName(goodsName);
        configDto.setCompanyId(companyId);
        if(ListUtil.isNotEmpty(iConfigDao.queryGoods(configDto))) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "货种名称已存在！");
            return retObj;
        }

        String parentGoodsId = JsonUtil.getJSONObjectString2(obj, "parentGoodsId");
        if (StringUtil.isNotEmpty(parentGoodsId)) {
            configDto.setParentId(Integer.parseInt(parentGoodsId));
        }

        configDto.setCompanyId(companyId);
        iConfigDao.insertGoods(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "添加成功");
        return retObj;
    }

    @Override
    public JSONObject deleteGoodsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl deleteGoodsConfig start obj:{}", obj);
        String goodsId = JsonUtil.getJSONObjectString2(obj, "goodsId");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(goodsId)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "参数为空");
            return retObj;
        }
        ConfigDto configDto = new ConfigDto();
        configDto.setGoodsId(Integer.parseInt(goodsId));
        configDto.setState("1");
        iConfigDao.updateGoods(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "删除成功");
        return retObj;
    }

    @Override
    public JSONObject editGoodsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl editGoodsConfig start obj:{}", obj);
        String goodsId = JsonUtil.getJSONObjectString2(obj, "goodsId");
        String goodsName = JsonUtil.getJSONObjectString2(obj, "goodsName");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(goodsId) || StringUtil.isEmpty(goodsName)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "请选择货种！");
        }
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        ConfigDto configDto = new ConfigDto();
        configDto.setGoodsName(goodsName);
        configDto.setCompanyId(companyId);
        if(ListUtil.isNotEmpty(iConfigDao.queryGoods(configDto))) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "货种名称已存在！");
            return retObj;
        }
        configDto.setGoodsId(Integer.parseInt(goodsId));
        iConfigDao.updateGoods(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "修改成功！");
        return retObj;
    }

    @Override
    public JSONObject queryGoodsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl queryGoodsConfig start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        ConfigDto configDto = new ConfigDto();
        String goodsId = JsonUtil.getJSONObjectString2(obj, "goodsId");
        if (StringUtil.isNotEmpty(goodsId)) {
            configDto.setGoodsId(Integer.parseInt(goodsId));
        }
        configDto.setCompanyId(companyId);
        List<ConfigDto> list = iConfigDao.queryGoods(configDto);
        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject obj1 = new JSONObject();
            obj1.put("goodsName", configDto1.getGoodsName());
            obj1.put("goodsId", configDto1.getGoodsId());
            jsonArray.add(obj1);
        }
        retObj.put("data", jsonArray);
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject queryParentGoodsConfig(HttpServletRequest request) {
        logger.info("ConfigServiceImpl queryParentGoodsConfig start");
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        ConfigDto configDto = new ConfigDto();
        configDto.setCompanyId(companyId);
        configDto.setParentIdNN("Y");
        List<ConfigDto> list = iConfigDao.queryGoods(configDto);
        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goodsId", configDto1.getGoodsId());
            jsonObject.put("goodsName", configDto1.getGoodsName());
            jsonArray.add(jsonObject);
        }
        retObj.put("data", jsonArray);
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject queryAllGoodsConfig(HttpServletRequest request, String page, String limit) throws Exception {
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));

        ParamDto paramDto = new ParamDto();
        paramDto.setCompanyId(companyId);
        PageUtil.setPage(Integer.parseInt(page),Integer.parseInt(limit), paramDto);

        List<ConfigDto> list = iConfigDao.queryAllGoods(paramDto);
        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goodsId", configDto1.getGoodsId());
            jsonObject.put("goodsName", configDto1.getGoodsName());
            jsonArray.add(jsonObject);
        }
        retObj.put("data", jsonArray);
        retObj.put("count", iConfigDao.queryAllGoodsCount(paramDto));
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject addHuoZhuConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl addHuoZhuConfig start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));

        String huoZhuName = JsonUtil.getJSONObjectString2(obj, "huoZhuName");
        String contactPhone = JsonUtil.getJSONObjectString(obj, "contactPhone");
        String contactMan = JsonUtil.getJSONObjectString(obj, "contactMan");
        JSONObject retObj = new JSONObject();
        if (StringUtil.isEmpty(huoZhuName) || StringUtil.isEmpty(contactPhone) || StringUtil.isEmpty(contactMan)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "请填写货主信息！");
            return retObj;
        }

        ConfigDto configDto = new ConfigDto();
        configDto.setCompanyId(companyId);
        configDto.setHuoZhuName(huoZhuName);
        List<ConfigDto> list = iConfigDao.queryHuoZhu(configDto);

        if(ListUtil.isNotEmpty(list)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "货主名已存在！");
            return retObj;
        }

        configDto = new ConfigDto();
        configDto.setCompanyId(companyId);
        configDto.setContactPhone(contactPhone);
        list = iConfigDao.queryHuoZhu(configDto);
        retObj = new JSONObject();

        if(ListUtil.isNotEmpty(list)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "电话号码已存在！");
            return retObj;
        }

        configDto.setHuoZhuName(huoZhuName);
        configDto.setContactMan(contactMan);
        iConfigDao.insertHuoZhu(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "添加成功");
        return retObj;
    }

    @Override
    public JSONObject deleteHuoZhuConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl deleteHuoZhuConfig start obj:{}", obj);
        String huoZhuId = JsonUtil.getJSONObjectString2(obj, "huoZhuId");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(huoZhuId)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "删除失败，没有选择货主");
            return retObj;
        }
        ConfigDto configDto = new ConfigDto();
        configDto.setState("1");
        configDto.setHuoZhuId(Integer.parseInt(huoZhuId));
        iConfigDao.updateHuoZhu(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "删除成功");
        return retObj;
    }

    @Override
    public JSONObject editHuoZhuConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl editHuoZhuConfig start obj:{}", obj);
        String huoZhuId = JsonUtil.getJSONObjectString2(obj, "huoZhuId");
        String huoZhuName = JsonUtil.getJSONObjectString2(obj, "huoZhuName");
        String contactPhone = JsonUtil.getJSONObjectString2(obj, "contactPhone");
        String contactMan = JsonUtil.getJSONObjectString2(obj, "contactMan");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(huoZhuId) || StringUtil.isEmpty(huoZhuName) || StringUtil.isEmpty(contactPhone) || StringUtil.isEmpty(contactMan)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "数据不能为空");
            return retObj;
        }
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        ConfigDto configDto = new ConfigDto();
        configDto.setCompanyId(companyId);
        configDto.setHuoZhuName(huoZhuName);
        List<ConfigDto> list = iConfigDao.queryHuoZhu(configDto);

        if(ListUtil.isNotEmpty(list)) {
            if (!huoZhuId.equals(list.get(0).getHuoZhuId().toString())) {
                retObj.put("SUCCESS", false);
                retObj.put("MSG", "货主名已存在！");
                return retObj;
            }
        }

        configDto = new ConfigDto();
        configDto.setContactPhone(contactPhone);
        configDto.setCompanyId(companyId);
        list = iConfigDao.queryHuoZhu(configDto);
        if(ListUtil.isNotEmpty(list)) {
            if (!huoZhuId.equals(list.get(0).getHuoZhuId().toString())) {
                retObj.put("SUCCESS", false);
                retObj.put("MSG", "电话号码已存在！");
                return retObj;
            }

        }
        configDto.setHuoZhuId(Integer.parseInt(huoZhuId));
        configDto.setHuoZhuName(huoZhuName);
        configDto.setContactMan(contactMan);
        iConfigDao.updateHuoZhu(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "修改成功");
        return retObj;
    }

    @Override
    public JSONObject queryHuoZhuConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl queryHuoZhuConfig start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        List<ConfigDto> list;
        ConfigDto configDto = new ConfigDto();
        String huoZhuId = JsonUtil.getJSONObjectString2(obj, "huoZhuId");
        if (StringUtil.isNotEmpty(huoZhuId)) {
            configDto.setHuoZhuId(Integer.parseInt(huoZhuId));
        }
        configDto.setCompanyId(companyId);
        list = iConfigDao.queryHuoZhu(configDto);

        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("huoZhuName", configDto1.getHuoZhuName());
            jsonObject.put("huoZhuId", configDto1.getHuoZhuId());
            jsonObject.put("contactMan", configDto1.getContactMan());
            jsonObject.put("contactPhone", configDto1.getContactPhone());
            jsonArray.add(jsonObject);
        }
        retObj.put("data", jsonArray);
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject addLogisticsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl addLogisticsConfig start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        ConfigDto configDto = new ConfigDto();
        String logisticsName = JsonUtil.getJSONObjectString2(obj, "logisticsName");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(logisticsName)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "请填写物流名称！");
            return retObj;
        }
        configDto.setCompanyId(companyId);
        configDto.setLogisticsName(logisticsName);

        if(ListUtil.isNotEmpty(iConfigDao.queryLogistics(configDto))) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "物流名称已存在！");
            return retObj;
        }

        iConfigDao.insertLogistics(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "添加成功");
        return retObj;
    }

    @Override
    public JSONObject deleteLogisticsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl deleteLogisticsConfig start obj:{}", obj);
        String logisticsId = JsonUtil.getJSONObjectString2(obj, "logisticsId");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(logisticsId)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "参数为空");
            return retObj;
        }
        ConfigDto configDto = new ConfigDto();
        configDto.setState("1");
        configDto.setLogisticsId(Integer.parseInt(logisticsId));
        iConfigDao.updateLogistics(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "删除成功");
        return retObj;
    }

    @Override
    public JSONObject editLogisticsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl editLogisticsConfig start obj:{}", obj);
        String logisticsId = JsonUtil.getJSONObjectString2(obj, "logisticsId");
        String logisticsName = JsonUtil.getJSONObjectString2(obj, "logisticsName");
        JSONObject retObj = new JSONObject();
        if(StringUtil.isEmpty(logisticsId) || StringUtil.isEmpty(logisticsName)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "请填写物流名称！");
            return retObj;
        }

        ConfigDto configDto = new ConfigDto();
        configDto.setLogisticsName(logisticsName);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        configDto.setCompanyId(companyId);
        List<ConfigDto> list = iConfigDao.queryLogistics(configDto);
        if(ListUtil.isNotEmpty(list)) {
            if (!logisticsId.equals(list.get(0).getLogisticsId().toString())) {
                retObj.put("SUCCESS", false);
                retObj.put("MSG", "物流名称已存在！");
                return retObj;
            }
        }

        configDto.setLogisticsId(Integer.parseInt(logisticsId));
        iConfigDao.updateLogistics(configDto);

        retObj.put("SUCCESS", true);
        retObj.put("MSG", "修改成功");
        return retObj;
    }

    @Override
    public JSONObject queryLogisticsConfig(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl queryLogisticsConfig start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));

        List<ConfigDto> list;
        String page =JsonUtil.getJSONObjectString2(obj,"page");
        if (StringUtil.isNotEmpty(page)) {
            String limit = JsonUtil.getJSONObjectString2(obj,"limit");
            ParamDto paramDto = new ParamDto();
            paramDto.setCompanyId(companyId);
            PageUtil.setPage(Integer.parseInt(page),Integer.parseInt(limit), paramDto);
            list = iConfigDao.queryAllLogistics(paramDto);
        }
        else {
            ConfigDto configDto = new ConfigDto();
            String logisticsId = JsonUtil.getJSONObjectString(obj, "logisticsId");
            if (StringUtil.isNotEmpty(logisticsId)) {
                configDto.setLogisticsId(Integer.parseInt(logisticsId));
            }
            configDto.setCompanyId(companyId);
            list = iConfigDao.queryLogistics(configDto);
        }

        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("logisticsId", configDto1.getLogisticsId());
            jsonObject.put("logisticsName", configDto1.getLogisticsName());
            jsonArray.add(jsonObject);
        }
        retObj.put("data", jsonArray);
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject queryRole(HttpServletRequest request) {
        List<ConfigDto> list = iConfigDao.queryRole();
        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("roleId", configDto.getRoleId());
            jsonObject.put("roleName", configDto.getRoleName());
            jsonArray.add(jsonObject);
        }
        retObj.put("data", jsonArray);
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject queryCompanyBAS(HttpServletRequest request) {
        logger.info("ConfigServiceImpl queryCompanyBAS start");
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));

        ConfigDto configDto = new ConfigDto();
        configDto.setCompanyId(companyId);
        List<ConfigDto> list = iConfigDao.queryAllCompanyBAS(configDto);
        if (ListUtil.isEmpty(list)) {
            List<ConfigDto> list1 = iConfigDao.queryAllBAS();
            for (ConfigDto configDto1 : list1) {
                configDto1.setCompanyId(companyId);
            }
            iConfigDao.insertCompanyBAS(list1);
            list = iConfigDao.queryAllCompanyBAS(configDto);
        }

        JSONObject retObj = new JSONObject();
        retObj.put("companyBASList", list);
        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public JSONObject editCompanyBAS(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("ConfigServiceImpl editCompanyBAS start obj:{}", obj);
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        String analysisSwitchId =JsonUtil.getJSONObjectString2(obj,"analysisSwitchId");
        String analysisSwitchState =JsonUtil.getJSONObjectString2(obj,"state");
        JSONObject retObj = new JSONObject();
        if (StringUtil.isEmpty(analysisSwitchId) || StringUtil.isEmpty(analysisSwitchState)) {
            retObj.put("SUCCESS", false);
            retObj.put("MSG", "修改失败！数据出错！");
            return retObj;
        }
        ConfigDto configDto = new ConfigDto();
        configDto.setCompanyId(companyId);
        configDto.setAnalysisSwitchId(Integer.parseInt(analysisSwitchId));
        configDto.setState(analysisSwitchState.toUpperCase());
        iConfigDao.updateCompanyBAS(configDto);

        retObj.put("SUCCESS", true);
        return retObj;
    }

    @Override
    public Map<String, Object> queryHuoZhuConfigPage(HttpServletRequest request, String page, String limit) {
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        List<ConfigDto> list;
        ParamDto paramDto = new ParamDto();
        paramDto.setCompanyId(companyId);
        PageUtil.setPage(Integer.parseInt(page),Integer.parseInt(limit), paramDto);
        list = iConfigDao.queryAllHuoZhu(paramDto);

        Map<String, Object> result = new HashMap<>();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("huoZhuName", configDto1.getHuoZhuName());
            jsonObject.put("huoZhuId", configDto1.getHuoZhuId());
            jsonObject.put("contactMan", configDto1.getContactMan());
            jsonObject.put("contactPhone", configDto1.getContactPhone());
            jsonArray.add(jsonObject);
        }
        result.put("data", jsonArray);
        result.put("SUCCESS", true);
        result.put("count", iConfigDao.queryAllHuoZhuCount(paramDto));
        return result;
    }

    @Override
    public Map<String, Object> queryLogisticsConfigPage(HttpServletRequest request, String page, String limit) {
        int companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));

        ParamDto paramDto = new ParamDto();
        paramDto.setCompanyId(companyId);
        PageUtil.setPage(Integer.parseInt(page),Integer.parseInt(limit), paramDto);
        List<ConfigDto> list = iConfigDao.queryAllLogistics(paramDto);

        JSONObject retObj = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (ConfigDto configDto1 : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("logisticsId", configDto1.getLogisticsId());
            jsonObject.put("logisticsName", configDto1.getLogisticsName());
            jsonArray.add(jsonObject);
        }
        retObj.put("data", jsonArray);
        retObj.put("count", iConfigDao.queryAllLogisticsCount(paramDto));
        retObj.put("SUCCESS", true);
        return retObj;
    }

}
